<template>
    <div>
        <el-row>
            <el-col span="4"> </el-col>
            <el-col span="20">
                <div class="crumbs">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 日志列表 </el-breadcrumb-item>
                    </el-breadcrumb>
                </div>
                <div class="container">
                    <div class="handle-box">
                        <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="baatchexport">批量导出</el-button>
                        <el-input v-model="query.UserName" placeholder="用户名" class="handle-input mr10"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="getData">搜索</el-button>
                    </div>

                    <el-table
                        :data="tableData"
                        border
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column prop="Content" label="日志内容" width="180"> </el-table-column>
                        <el-table-column prop="Classify" label="类别" width="180"> </el-table-column>
                        <el-table-column prop="Use" label="应用" width="180"> </el-table-column>
                        <el-table-column prop="Module" label="路径" width="180"> </el-table-column>
                        <el-table-column prop="Whether" label="状态" width="180">
                            <template slot-scope="scope">
                                <span v-if="scope.row.Whether == true"><el-button type="success" round>正常</el-button></span>
                                <span v-if="scope.row.Whether == false"><el-button type="danger" round>异常</el-button></span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="Createtime" label="时间" width="180"> </el-table-column>
                        <el-table-column prop="OperatorName" label="操作人" width="180"> </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <el-pagination
                            background
                            layout="total, prev, pager, next"
                            :current-page="query.pageIndex"
                            :page-size="query.pageSize"
                            :total="pageTotal"
                            @current-change="handlePageChange"
                        ></el-pagination>
                    </div>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import {
    userrole,
    rolelist,
    addrelation,
    deleterelation,
    permission,
    addpermiss,
    rolepermiss,
    findpermiss,
    navpermiss,
    addpermission,
    delpermission,
    backpermission,
    updpermission,
    logMessagelist
} from '@/api/role';
import Treeselect from '@riophae/vue-treeselect'; //树下拉
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
export default {
    components: { Treeselect },
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                UserName: '',
                pageIndex: 1,
                pageSize: 10
            },
            roleData: [],
            tableData: [],
            upddialogVisible: false,
            multipleSelection: [],
            delList: [],
            dialogVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            data: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            value: null,
            options: [],
            forms: {}
        };
    },
    created() {
        this.getData();
        this.PermissionList();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            logMessagelist({ pageIndex: this.query.pageIndex, pageSize: this.query.pageSize }).then((res) => {
                //这里的res其实就是res.ata对象，因为拦截器里面返回了res.data
                console.log(res);
                this.tableData = res.data;
                this.tableData = res.data.List;
                this.pageTotal = res.data.Total;
                //this.query.name=res.data.UserName
            });
        },
        //批量导出
        baatchexport() {
            //window.location.href=url;
            //方法二 文件流下载
            this.$axios({
                method: 'get',
                url: 'http://localhost:5086/api/SysLogs/Export',
                params: {
                    pageIndex: this.query.pageIndex,
                    pageSize: this.query.pageSize
                    //this.queryParams
                },
                headers: {
                    Authorization: 'Bearer ' + sessionStorage.getItem('jwtToken')
                },
                responseType: 'blob'
            }).then((response) => {
                //获取文件名
                var content = response.headers['content-disposition'];
                var name = content && content.split(';')[2].split("filename*=UTF-8''")[1];
                var fileName = decodeURIComponent(name);
                //获取文件流
                let blob = new Blob([response.data], { type: 'application/octet-stream' });
                let url = window.URL.createObjectURL(blob);
                console.log(url, fileName);
                //动态创建a标签
                let link = document.createElement('a');
                link.href = url;
                link.style.display = 'none';
                link.setAttribute('download', fileName);
                document.body.appendChild(link);
                link.click(); //点击a标签
                window.URL.revokeObjectURL(url);
                document.body.removeChild(link);
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        //树形
        handleNodeClick(data) {
            console.log('树数据', data);
        },
        // 编辑操作
        handleEdit(index, row) {
            this.editVisible = true;

            this.idx = index;
            this.form.Id = row.Id;
            this.form.CreateTime = row.CreateTime;
            // this.editVisible = true;
        },
        // 保存编辑
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        //树下拉
        PermissionList() {
            permission().then((res) => {
                if (res.code == 200) {
                    this.options = res.data;
                }
            });
        },
        //添加权限
        addPermiss() {
            this.dialogVisible = true;
        },
        addpermission() {
            this.form.Pid = this.form.Pid[0];
            console.log('父id', this.form.Pid);
            console.log('添加权限的信息', this.form);
            addpermission(this.form).then((res) => {
                if (res.code == 200) {
                    this.$message.success('添加权限成功');
                    this.dialogVisible = false;
                    this.getData();
                }
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
